home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxdcolor.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  11.8 KB  |  393 lines

  1. /* Copyright (C) 1996, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxdcolor.c,v 1.3 2000/09/19 19:00:35 lpd Exp $ */
  20. /* Pure and null device color implementation */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gsbittab.h"
  24. #include "gxdcolor.h"
  25. #include "gxdevice.h"
  26.  
  27. /* Define the standard device color types. */
  28.  
  29. /* 'none' means the color is not defined. */
  30. private dev_color_proc_load(gx_dc_no_load);
  31. private dev_color_proc_fill_rectangle(gx_dc_no_fill_rectangle);
  32. private dev_color_proc_fill_masked(gx_dc_no_fill_masked);
  33. private dev_color_proc_equal(gx_dc_no_equal);
  34. const gx_device_color_type_t gx_dc_type_data_none = {
  35.     &st_bytes,
  36.     gx_dc_no_load, gx_dc_no_fill_rectangle, gx_dc_no_fill_masked,
  37.     gx_dc_no_equal
  38. };
  39. #undef gx_dc_type_none
  40. const gx_device_color_type_t *const gx_dc_type_none = &gx_dc_type_data_none;
  41. #define gx_dc_type_none (&gx_dc_type_data_none)
  42.  
  43. /* 'null' means the color has no effect when used for drawing. */
  44. private dev_color_proc_load(gx_dc_null_load);
  45. private dev_color_proc_fill_rectangle(gx_dc_null_fill_rectangle);
  46. private dev_color_proc_fill_masked(gx_dc_null_fill_masked);
  47. private dev_color_proc_equal(gx_dc_null_equal);
  48. const gx_device_color_type_t gx_dc_type_data_null = {
  49.     &st_bytes,
  50.     gx_dc_null_load, gx_dc_null_fill_rectangle, gx_dc_null_fill_masked,
  51.     gx_dc_null_equal
  52. };
  53. #undef gx_dc_type_null
  54. const gx_device_color_type_t *const gx_dc_type_null = &gx_dc_type_data_null;
  55. #define gx_dc_type_null (&gx_dc_type_data_null)
  56.  
  57. private dev_color_proc_load(gx_dc_pure_load);
  58. private dev_color_proc_fill_rectangle(gx_dc_pure_fill_rectangle);
  59. private dev_color_proc_fill_masked(gx_dc_pure_fill_masked);
  60. private dev_color_proc_equal(gx_dc_pure_equal);
  61. const gx_device_color_type_t gx_dc_type_data_pure = {
  62.     &st_bytes,
  63.     gx_dc_pure_load, gx_dc_pure_fill_rectangle, gx_dc_pure_fill_masked,
  64.     gx_dc_pure_equal
  65. };
  66. #undef gx_dc_type_pure
  67. const gx_device_color_type_t *const gx_dc_type_pure = &gx_dc_type_data_pure;
  68. #define gx_dc_type_pure (&gx_dc_type_data_pure)
  69.  
  70. /*
  71.  * Get the black and white pixel values of a device.  The documentation for
  72.  * the driver API says that map_rgb_color will do the right thing on CMYK
  73.  * devices.  Unfortunately, that isn't true at present, and fixing it is too
  74.  * much work.
  75.  */
  76. gx_color_index
  77. gx_device_black(gx_device *dev)
  78. {
  79.     if (dev->cached_colors.black == gx_no_color_index)
  80.     dev->cached_colors.black =
  81.         (dev->color_info.num_components == 4 ?
  82.          (*dev_proc(dev, map_cmyk_color))
  83.          (dev, (gx_color_index)0, (gx_color_index)0, (gx_color_index)0,
  84.           gx_max_color_value) :
  85.          (*dev_proc(dev, map_rgb_color))
  86.          (dev, (gx_color_index)0, (gx_color_index)0, (gx_color_index)0));
  87.     return dev->cached_colors.black;
  88. }
  89. gx_color_index
  90. gx_device_white(gx_device *dev)
  91. {
  92.     if (dev->cached_colors.white == gx_no_color_index)
  93.     dev->cached_colors.white =
  94.         (dev->color_info.num_components == 4 ?
  95.          (*dev_proc(dev, map_cmyk_color))
  96.          (dev, (gx_color_index)0, (gx_color_index)0, (gx_color_index)0,
  97.           (gx_color_index)0) :
  98.          (*dev_proc(dev, map_rgb_color))
  99.          (dev, gx_max_color_value, gx_max_color_value, gx_max_color_value));
  100.     return dev->cached_colors.white;
  101. }
  102.  
  103. /* Clear the color cache. */
  104. void
  105. gx_device_decache_colors(gx_device *dev)
  106. {
  107.     dev->cached_colors.black = dev->cached_colors.white = gx_no_color_index;
  108. }
  109.  
  110. /* Set a null RasterOp source. */
  111. private const gx_rop_source_t gx_rop_no_source_0 = {gx_rop_no_source_body(0)};
  112. private const gx_rop_source_t gx_rop_no_source_1 = {gx_rop_no_source_body(1)};
  113. void
  114. gx_set_rop_no_source(const gx_rop_source_t **psource,
  115.              gx_rop_source_t *pno_source, gx_device *dev)
  116. {
  117.     gx_color_index black;
  118.  
  119. top:
  120.     black = dev->cached_colors.black;
  121.     if (black == 0)
  122.     *psource = &gx_rop_no_source_0;
  123.     else if (black == 1)
  124.     *psource = &gx_rop_no_source_1;
  125.     else if (black == gx_no_color_index) {    /* cache not loaded */
  126.     discard(gx_device_black(dev));
  127.     goto top;
  128.     } else {
  129.     *pno_source = gx_rop_no_source_0;
  130.     gx_rop_source_set_color(pno_source, black);
  131.     *psource = pno_source;
  132.     }
  133. }
  134.  
  135. /* Test device colors for equality. */
  136. bool
  137. gx_device_color_equal(const gx_device_color *pdevc1,
  138.               const gx_device_color *pdevc2)
  139. {
  140.     return pdevc1->type->equal(pdevc1, pdevc2);
  141. }
  142.  
  143. /* ------ Undefined color ------ */
  144.  
  145. private int
  146. gx_dc_no_load(gx_device_color *pdevc, const gs_imager_state *ignore_pis,
  147.           gx_device *ignore_dev, gs_color_select_t ignore_select)
  148. {
  149.     return 0;
  150. }
  151.  
  152. private int
  153. gx_dc_no_fill_rectangle(const gx_device_color *pdevc, int x, int y,
  154.             int w, int h, gx_device *dev,
  155.             gs_logical_operation_t lop,
  156.             const gx_rop_source_t *source)
  157. {
  158.     gx_device_color filler;
  159.  
  160.     if (w <= 0 || h <= 0)
  161.     return 0;
  162.     if (lop_uses_T(lop))
  163.     return_error(gs_error_Fatal);
  164.     color_set_pure(&filler, 0);     /* any valid value for dev will do */
  165.     return gx_dc_pure_fill_rectangle(&filler, x, y, w, h, dev, lop, source);
  166. }
  167.  
  168. private int
  169. gx_dc_no_fill_masked(const gx_device_color *pdevc, const byte *data,
  170.              int data_x, int raster, gx_bitmap_id id,
  171.              int x, int y, int w, int h, gx_device *dev,
  172.              gs_logical_operation_t lop, bool invert)
  173. {
  174.     if (w <= 0 || h <= 0)
  175.     return 0;
  176.     return_error(gs_error_Fatal);
  177. }
  178.  
  179. private bool
  180. gx_dc_no_equal(const gx_device_color *pdevc1, const gx_device_color *pdevc2)
  181. {
  182.     return false;
  183. }
  184.  
  185. /* ------ Null color ------ */
  186.  
  187. private int
  188. gx_dc_null_load(gx_device_color *pdevc, const gs_imager_state *ignore_pis,
  189.         gx_device *ignore_dev, gs_color_select_t ignore_select)
  190. {
  191.     return 0;
  192. }
  193.  
  194. private int
  195. gx_dc_null_fill_rectangle(const gx_device_color * pdevc, int x, int y,
  196.               int w, int h, gx_device * dev,
  197.               gs_logical_operation_t lop,
  198.               const gx_rop_source_t * source)
  199. {
  200.     return 0;
  201. }
  202.  
  203. private int
  204. gx_dc_null_fill_masked(const gx_device_color * pdevc, const byte * data,
  205.                int data_x, int raster, gx_bitmap_id id,
  206.                int x, int y, int w, int h, gx_device * dev,
  207.                gs_logical_operation_t lop, bool invert)
  208. {
  209.     return 0;
  210. }
  211.  
  212. private bool
  213. gx_dc_null_equal(const gx_device_color * pdevc1, const gx_device_color * pdevc2)
  214. {
  215.     return pdevc2->type == pdevc1->type;
  216. }
  217.  
  218. /* ------ Pure color ------ */
  219.  
  220. private int
  221. gx_dc_pure_load(gx_device_color * pdevc, const gs_imager_state * ignore_pis,
  222.         gx_device * ignore_dev, gs_color_select_t ignore_select)
  223. {
  224.     return 0;
  225. }
  226.  
  227. /* Fill a rectangle with a pure color. */
  228. /* Note that we treat this as "texture" for RasterOp. */
  229. private int
  230. gx_dc_pure_fill_rectangle(const gx_device_color * pdevc, int x, int y,
  231.           int w, int h, gx_device * dev, gs_logical_operation_t lop,
  232.               const gx_rop_source_t * source)
  233. {
  234.     if (source == NULL && lop_no_S_is_T(lop))
  235.     return (*dev_proc(dev, fill_rectangle)) (dev, x, y, w, h,
  236.                          pdevc->colors.pure);
  237.     {
  238.     gx_color_index colors[2];
  239.     gx_rop_source_t no_source;
  240.  
  241.     colors[0] = colors[1] = pdevc->colors.pure;
  242.     if (source == NULL)
  243.         set_rop_no_source(source, no_source, dev);
  244.     return (*dev_proc(dev, strip_copy_rop))
  245.         (dev, source->sdata, source->sourcex, source->sraster,
  246.          source->id, (source->use_scolors ? source->scolors : NULL),
  247.          NULL /*arbitrary */ , colors, x, y, w, h, 0, 0, lop);
  248.     }
  249. }
  250.  
  251. /* Fill a mask with a pure color. */
  252. /* Note that there is no source in this case: the mask is the source. */
  253. private int
  254. gx_dc_pure_fill_masked(const gx_device_color * pdevc, const byte * data,
  255.     int data_x, int raster, gx_bitmap_id id, int x, int y, int w, int h,
  256.            gx_device * dev, gs_logical_operation_t lop, bool invert)
  257. {
  258.     if (lop_no_S_is_T(lop)) {
  259.     gx_color_index color0, color1;
  260.  
  261.     if (invert)
  262.         color0 = pdevc->colors.pure, color1 = gx_no_color_index;
  263.     else
  264.         color1 = pdevc->colors.pure, color0 = gx_no_color_index;
  265.     return (*dev_proc(dev, copy_mono))
  266.         (dev, data, data_x, raster, id, x, y, w, h, color0, color1);
  267.     } {
  268.     gx_color_index scolors[2];
  269.     gx_color_index tcolors[2];
  270.  
  271.     scolors[0] = gx_device_black(dev);
  272.     scolors[1] = gx_device_white(dev);
  273.     tcolors[0] = tcolors[1] = pdevc->colors.pure;
  274.     return (*dev_proc(dev, strip_copy_rop))
  275.         (dev, data, data_x, raster, id, scolors,
  276.          NULL, tcolors, x, y, w, h, 0, 0,
  277.          (invert ? rop3_invert_S(lop) : lop) | lop_S_transparent);
  278.     }
  279. }
  280.  
  281. private bool
  282. gx_dc_pure_equal(const gx_device_color * pdevc1, const gx_device_color * pdevc2)
  283. {
  284.     return pdevc2->type == pdevc1->type &&
  285.     gx_dc_pure_color(pdevc1) == gx_dc_pure_color(pdevc2);
  286. }
  287.  
  288. /* ------ Halftone color initialization ------ */
  289.  
  290. void
  291. gx_complete_rgb_halftone(gx_device_color *pdevc, gx_device_halftone *pdht)
  292. {
  293.     pdevc->type = gx_dc_type_ht_colored;
  294.     pdevc->colors.colored.c_ht = pdht;
  295.     pdevc->colors.colored.plane_mask =
  296.     (pdevc->colors.colored.c_level[0] != 0) |
  297.     ((pdevc->colors.colored.c_level[1] != 0) << 1) |
  298.     ((pdevc->colors.colored.c_level[2] != 0) << 2);
  299.     /*
  300.      * Color rendering won't use the fourth component, but the code that
  301.      * writes and reads colored halftones in the band list doesn't know that.
  302.      */
  303.     pdevc->colors.colored.c_base[3] = 0;
  304.     pdevc->colors.colored.c_level[3] = 0;
  305. }
  306.  
  307. void
  308. gx_complete_cmyk_halftone(gx_device_color *pdevc, gx_device_halftone *pdht)
  309. {
  310.     pdevc->type = gx_dc_type_ht_colored;
  311.     pdevc->colors.colored.c_ht = pdht;
  312.     pdevc->colors.colored.alpha = max_ushort;
  313.     pdevc->colors.colored.plane_mask =
  314.     (pdevc->colors.colored.c_level[0] != 0) |
  315.     ((pdevc->colors.colored.c_level[1] != 0) << 1) |
  316.     ((pdevc->colors.colored.c_level[2] != 0) << 2) |
  317.     ((pdevc->colors.colored.c_level[3] != 0) << 3);
  318. }
  319.  
  320. /* ------ Default implementations ------ */
  321.  
  322. /* Fill a mask with a color by parsing the mask into rectangles. */
  323. int
  324. gx_dc_default_fill_masked(const gx_device_color * pdevc, const byte * data,
  325.     int data_x, int raster, gx_bitmap_id id, int x, int y, int w, int h,
  326.            gx_device * dev, gs_logical_operation_t lop, bool invert)
  327. {
  328.     int lbit = data_x & 7;
  329.     const byte *row = data + (data_x >> 3);
  330.     uint one = (invert ? 0 : 0xff);
  331.     uint zero = one ^ 0xff;
  332.     int iy;
  333.  
  334.     for (iy = 0; iy < h; ++iy, row += raster) {
  335.     const byte *p = row;
  336.     int bit = lbit;
  337.     int left = w;
  338.     int l0;
  339.  
  340.     while (left) {
  341.         int run, code;
  342.  
  343.         /* Skip a run of zeros. */
  344.         run = byte_bit_run_length[bit][*p ^ one];
  345.         if (run) {
  346.         if (run < 8) {
  347.             if (run >= left)
  348.             break;    /* end of row while skipping */
  349.             bit += run, left -= run;
  350.         } else if ((run -= 8) >= left)
  351.             break;    /* end of row while skipping */
  352.         else {
  353.             left -= run;
  354.             ++p;
  355.             while (left > 8 && *p == zero)
  356.             left -= 8, ++p;
  357.             run = byte_bit_run_length_0[*p ^ one];
  358.             if (run >= left)    /* run < 8 unless very last byte */
  359.             break;    /* end of row while skipping */
  360.             else
  361.             bit = run & 7, left -= run;
  362.         }
  363.         }
  364.         l0 = left;
  365.         /* Scan a run of ones, and then paint it. */
  366.         run = byte_bit_run_length[bit][*p ^ zero];
  367.         if (run < 8) {
  368.         if (run >= left)
  369.             left = 0;
  370.         else
  371.             bit += run, left -= run;
  372.         } else if ((run -= 8) >= left)
  373.         left = 0;
  374.         else {
  375.         left -= run;
  376.         ++p;
  377.         while (left > 8 && *p == one)
  378.             left -= 8, ++p;
  379.         run = byte_bit_run_length_0[*p ^ zero];
  380.         if (run >= left)    /* run < 8 unless very last byte */
  381.             left = 0;
  382.         else
  383.             bit = run & 7, left -= run;
  384.         }
  385.         code = gx_device_color_fill_rectangle(pdevc,
  386.               x + w - l0, y + iy, l0 - left, 1, dev, lop, NULL);
  387.         if (code < 0)
  388.         return code;
  389.     }
  390.     }
  391.     return 0;
  392. }
  393.